package com.hmusic.mymusic.service.impl;

import com.hmusic.mymusic.dao.ListSongMapper;
import com.hmusic.mymusic.dao.SongListMapper;
import com.hmusic.mymusic.domain.Song;
import com.hmusic.mymusic.domain.SongList;
import com.hmusic.mymusic.service.SongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 歌单service实现类
 */
@Service
public class SongListServiceImpl implements SongListService {

    @Autowired
    private SongListMapper songListMapper;
    @Autowired
    private ListSongMapper listSongMapper;
    @Value("${songListPic.dir}")
    private String realPath;

    /**
     * 添加歌单
     *
     * @param songList
     */
    @Override
    public void insert(SongList songList) {
        songListMapper.insert(songList);
    }

    /**
     * 修改歌单
     *
     * @param songList
     */
    @Override
    public void update(SongList songList) {
        songListMapper.update(songList);
    }

    /**
     * 删除歌单
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        try{
            SongList songList = songListMapper.selectByPrimaryKey(id);
            String oldSongListAvatorUrl = songList.getPic();
            String newSongListAvatorUrl = oldSongListAvatorUrl.substring(17);
            if (!newSongListAvatorUrl.equals("FirstSongListPic.jpeg")){
                File file=new File(realPath,newSongListAvatorUrl);
                if(file.exists())file.delete();//删除原有图片地址
            }
            listSongMapper.deleteBySongListId(id);
            songListMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据主键查询整个对象
     *
     * @param id
     */
    @Override
    public SongList selectByPrimaryKey(String id) {
        try {
            songListMapper.updateClick(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return songListMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有歌单
     */
    @Override
    public List<SongList> allSongList() {
        return songListMapper.allSongList();
    }

    /**
     * 根据歌单名模糊查询
     *
     * @param title
     */
    @Override
    public List<SongList> likeTitle(String title) {
        return songListMapper.likeTitle(title);
    }

    /**
     * 根据歌单名精确查询
     *
     * @param title
     */
    @Override
    public List<SongList> songListOfName(String title) {
        return songListMapper.songListOfName(title);
    }

    /**
     * 根据风格模糊查询
     *
     * @param style
     */
    @Override
    public List<SongList> likeStyle(String style) {
        return songListMapper.likeStyle(style);
    }

    /**
     * 计算歌单数量
     */
    @Override
    public int songListCount() {
        return songListMapper.songListCount();
    }

    /**
     * 首页热门歌单
     *
     * @return
     */
    @Override
    public List<SongList> hotSongList() {
        return songListMapper.hotSongList();
    }

    /**
     * 点击量增加
     *
     * @param id
     */
    @Override
    public void updateClick(String id) {
        songListMapper.updateClick(id);
    }
}
